package com.mx.kn.cstf2.actions;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import kn.cstf2.assign.Bean.ListaTablaBean;
import kn.cstf2.assign.Server.AssignServer;
import kn.cstf2.global.Utils.CstF2Exception;
import kn.cstf2.main.Server.MainServer;
import kn.cstf2.main.Shared.MainShared;
import kn.cstf2.transition.Bean.TransitionBean;
import com.mx.kn.cstf2.dto.UsuarioDto;
import com.mx.kn.cstf2.service.AssignService;
import com.mx.kn.cstf2.service.MainService;
import com.mx.kn.cstf2.service.TransitionService;

import org.jfree.util.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * @author user1
 */
@Component("mainAction")
@Scope(value = "session")
public class MainAction extends ActionSupport {

	@Autowired
	AssignService assignService;
	@Autowired
	MainService mainService;
	@Autowired
	TransitionService transitionService;

	public static final String ERROR_SESSION = "errorSession";
	private TransitionBean transitionBean = new TransitionBean();
	private ArrayList<TransitionBean> transitionBeanArray;
	private ArrayList<ListaTablaBean> listaTablaBeans;
	private String knkey;
	private Integer knkey2;
	private String companyName;
	private ListaTablaBean knKeyFound;
	private UsuarioDto usuario = new UsuarioDto();
	private Integer idTransition;

	public String getKnkey() {
		return knkey;
	}

	public void setKnkey(String knkey) {
		this.knkey = knkey;
	}

	public ArrayList<TransitionBean> getTransitionBeanArray() {
		return transitionBeanArray;
	}

	public void setTransitionBeanArray(
			ArrayList<TransitionBean> transitionBeanArray) {
		this.transitionBeanArray = transitionBeanArray;
	}

	public ArrayList<ListaTablaBean> getListaTablaBeans() {
		return assignService.obtenerListaTabla(getUsuario().getId());
	}

	public void setListaTablaBeans(ArrayList<ListaTablaBean> listaTablaBeans) {
		this.listaTablaBeans = listaTablaBeans;
	}

	public Integer getKnkey2() {
		return knkey2;
	}

	public void setKnkey2(Integer knkey2) {
		this.knkey2 = knkey2;
	}

	public MainAction() {
//
//		ActionContext contexto = ActionContext.getContext();
//		AssignServer assignServer = new AssignServer();
//		Map<String, Object> sesion = contexto.getSession();
//		this.setUsuario((UsuarioDto) sesion.get("usuarioSession"));
//
//		MainShared mainShared = new MainShared();
//		setTransitionBeanArray(mainShared
//				.obtenerKnkeysandClientNames(getUsuario()));
	}

	@Override
	public String execute() {

		try {
			if (transitionBean != null) {
				ActionContext contexto = ActionContext.getContext();
				Map<String, Object> sesion = contexto.getSession();
				setUsuario((UsuarioDto) sesion.get("usuarioSession"));
				cargarKnkeyOnSession();
			} else {

//				addFieldError("KnkeyIncorrecta", "No hay Knkeys asignadas.");
				return ERROR_SESSION;
			}
		} catch (CstF2Exception ex) {

//			addFieldError("KnkeyIncorrecta",
//					"Ocurrio un Error: " + ex.getMessage());
			return ERROR_SESSION;
		}
		return SUCCESS;
	}

	public void cargarKnkeyOnSession() {
		TransitionBean transitionBean = new TransitionBean();
		if (knkey2 != null) {
			transitionBean.setId(idTransition);
			transitionBean.setKnkey(knkey2);

			ActionContext contexto = ActionContext.getContext();
			Map<String, Object> sesion2 = contexto.getSession();
			sesion2.put("Knkey", transitionBean);
			mainService.updatedFalse(knkey2);
		}
	}

	public String searchKnKey() {

		try {
			ListaTablaBean listaTable = mainService.getCliente(Integer
					.parseInt(knkey));
			if (listaTable.getKnkey() != null)
				this.setKnKeyFound(mainService.getCliente(Integer
						.parseInt(knkey)));
//			else
//				addFieldError("error", "Key no existe");

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NumberFormatException nFE) {
//			addFieldError("error", "Key  no valida");
			Log.info("no es numero");

		}
		return SUCCESS;
	}

	public String entradaHouseAccount() {

		// checar si transaction ya existe
		try {
			TransitionBean transitionBean = new TransitionBean();
			transitionBean.setKnkey(knkey2);
			ActionContext contexto = ActionContext.getContext();
			Map<String, Object> sesion = contexto.getSession();
			sesion.put("Knkey", transitionBean);

			if (!transitionService.checkIfExistTransaction(Integer
					.parseInt(knkey))) {
				transitionService.newTransition(knkey2, companyName,this.getUsuario());

			}

		} catch (NumberFormatException e) {

			e.printStackTrace();
		} catch (SQLException e) {

			e.printStackTrace();
		}

		return SUCCESS;
	}

	public UsuarioDto getUsuario() {
		return usuario;
	}

	public void setUsuario(UsuarioDto usuario) {
		this.usuario = usuario;
	}

	public ListaTablaBean getKnKeyFound() {
		return knKeyFound;
	}

	public void setKnKeyFound(ListaTablaBean knKeyFound) {
		this.knKeyFound = knKeyFound;
	}

	public String getCompanyName() {
		return companyName;
	}

	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}

	public Integer getIdTransition() {
		return idTransition;
	}

	public void setIdTransition(Integer idTransition) {
		this.idTransition = idTransition;
	}
	
	

}
